if TUNING.NPC_UPGRADE_ITEMS_FN == nil then
    TUNING.NPC_UPGRADE_ITEMS_FN = {}
end


local function Fn()
    local Fx = Class()

    
    local function GetStringsTable()
        return TUNING.NPC_CHAT_TABLE[ type(TUNING.__NPC_LANGUAGE)=="function" and TUNING.__NPC_LANGUAGE() or TUNING.__NPC_LANGUAGE ] ["cane_upgrade"] or {}
    end
    function Fx:Init(inst)        
    end
    local function RemoveComponent_check(inst)
        --- 移除多余的组件，避免和其他MOD冲突
        local init_com_list = {
            ["inspectable"] = true,
            ["inventoryitem"] = true,
            ["weapon"] = true,
            ["equippable"] = true,
            ["inventoryitemmoisture"] = true,
            ["hauntable"] = true,
            ["floater"] = true,
            ["npc_base_lib"] = true,
            ["npc_everything_data"] = true,
            ["named"] = true,
        }        
        for k, v in pairs(inst.components) do
            if init_com_list[k] ~= true then
                inst:RemoveComponent(k)
            end
        end
    end
    function Fx:upgrade_fireflies(inst) ----------------------------- 萤火虫手杖
        RemoveComponent_check(inst)
        --------------------------------------------------------------------------
        inst.components.named:SetName(GetStringsTable().name_fireflies)

        inst.___fireflies_tree = inst:SpawnChild("npc_item_fireflies_tree")
        inst.___fireflies_tree.Ready = true
        inst.___fireflies_tree.Transform:SetPosition(0,0,0)
        -----------------------------------------------------------------

        local function Change_child_2_owner(inst,owner)
            local tree = inst.___fireflies_tree
            inst:RemoveChild(tree)
            owner:AddChild(tree)
            inst.___owner = owner
            inst.___fireflies_tree.Transform:SetPosition(0,0,0)

        end

        local function Change_child_back(inst)
            local tree = inst.___fireflies_tree
            local owner = inst.___owner
            if owner then
                owner:RemoveChild(tree)
            end
            inst:AddChild(tree)
            inst.___fireflies_tree.Transform:SetPosition(0,0,0)
        end

        inst:ListenForEvent("onpickup",function(inst,_table)
            if _table and _table.owner then
                Change_child_2_owner(inst,_table.owner)
            end
        end)
        inst:ListenForEvent("equipped",function(inst,_table)
            if _table and _table.owner then
                Change_child_2_owner(inst,_table.owner)
            end   
        end)

        inst:ListenForEvent("ondropped",function()
            Change_child_back(inst)
        end)
        inst:ListenForEvent("onputininventory",function()
            local owner = inst.npc_base_lib:Get_Owner()
            if owner then
                Change_child_2_owner(inst,owner)
            else
                Change_child_back(inst)
            end
        end)

        inst:DoTaskInTime(0.1,function()
            local owner = inst.npc_base_lib:Get_Owner()
            if owner then
                Change_child_2_owner(inst,owner)
            end
        end)

        --------------------------------------------------------------------------
        inst.Remove__old = inst.Remove
        inst.Remove = function(self)
            if inst.___fireflies_tree then
                inst.___fireflies_tree:Remove()
            end
            self:Remove__old()
        end
        --------------------------------------------------------------------------
        --------------------------------------------------------------------------
    end

    function Fx:upgrade_erode(inst) ---------------- 侵蚀手杖
        RemoveComponent_check(inst)
        --------------------------------------------------------------------------
        inst.components.named:SetName(GetStringsTable().name_erode)
        local speed_mult = TUNING.CANE_SPEED_MULT * 1.5
        speed_mult = speed_mult - speed_mult % 0.01
        inst.components.equippable.walkspeedmult = speed_mult
        local function Clean_last_owner_param(inst)
            if inst.____owner and inst.____owner.AnimState then
                inst.____owner.AnimState:SetErosionParams(0,0,0)
            end
            inst.____owner = nil
        end
        local function Set_Owner_Erode(inst)
            local owner = inst.npc_base_lib:Get_Owner(true)
            if owner then
                Clean_last_owner_param(inst)
                inst.____owner = owner
                if inst.____owner.AnimState then
                    inst.____owner.AnimState:SetErosionParams(0.1,-0.1,-1)
                end
            else
                Clean_last_owner_param(inst)
            end
        end
        -------------------------------------------------------------------------------------------
        inst:ListenForEvent("onputininventory",function()       --------- 进物品栏/背包的时候刷一下
            Set_Owner_Erode(inst)
        end)
        inst:ListenForEvent("ondropped",function()              ---------- 被丢出去的时候清理
            Clean_last_owner_param(inst)
        end)
        inst:ListenForEvent("equipped",function(inst,_table)    ---------- 穿戴的时候刷新
            inst:DoTaskInTime(0.1,function()
                Set_Owner_Erode(inst)
            end)
        end)

        inst:DoTaskInTime(0.1,function()                        ---------- 加载的时候刷新
            Set_Owner_Erode(inst)
        end)
        -------------------------------------------------------------------------------------------
        inst.Remove__old = inst.Remove
        inst.Remove = function(self,...)
            Clean_last_owner_param(inst)
            self:Remove__old()
        end
    end

    function Fx:upgrade_debug(inst) ---------------- 测试用的武器
        RemoveComponent_check(inst)
        --------------------------------------------------------------------------
        inst.components.named:SetName("debug")

        local function add2child(child,target)
            pcall(function()
                if target and child and child.parent ~= target then
                    target:AddChild(child)
                end
            end)
            
        end

        local function Clean_last_owner(inst)
            add2child(inst.butterflies,inst)
        end
        local function Set_Owner(inst)
            local owner = inst.npc_base_lib:Get_Owner(true)
            if owner then                
                add2child(inst.butterflies,owner)
            else
                add2child(inst,inst.butterflies)
            end
        end

        -------------------------------------------------------------------------------------------
            inst:ListenForEvent("onputininventory",function()       --------- 进物品栏/背包的时候刷一下
                Set_Owner(inst)
            end)
            inst:ListenForEvent("ondropped",function()              ---------- 被丢出去的时候清理
                Clean_last_owner(inst)
            end)
            inst:ListenForEvent("equipped",function(inst,_table)    ---------- 穿戴的时候刷新
                inst:DoTaskInTime(0.1,function()
                    Set_Owner(inst)
                end)
            end)

            inst:DoTaskInTime(0.1,function()                        ---------- 加载的时候刷新
                Set_Owner(inst)
            end)
        -------------------------------------------------------------------------------------------

        inst:DoTaskInTime(0.5,function()
                local butterflies = SpawnPrefab("npc_item_butterflies_cycle")
                inst.butterflies = butterflies
                butterflies.Ready = true
                inst.child = inst.child or {}
                inst:AddChild(butterflies)
                butterflies:DoTaskInTime(0.5,function()
                    butterflies:PushEvent("Spawn_butterflies")            
                end)
        
                local function OnAttack(inst,attacker,target)
                    if attacker == nil or attacker.components.playercontroller == nil or inst.butterflies == nil then
                        return
                    end
        
                    local target_fn = function(butterfly,owner,target)
                        print(butterfly,target)
                    end
        
                    inst.butterflies:PushEvent("attack",{
                        target_fn = target_fn,
                        target = target,
                    })
                end
                inst.components.weapon:SetOnAttack(OnAttack)
                inst.components.weapon:SetDamage(0) ----- 攻击力 为 0
                inst.components.weapon:SetRange(25) ----- 距离增加
        
        end)

        inst.Remove__old = inst.Remove
        inst.Remove = function(self,...)
            if inst.butterflies then
                inst.butterflies:Remove()
            end
            self:Remove__old()
        end
    end

    return Fx
end


if TUNING.NPC_UPGRADE_ITEMS_FN["cane"] == nil then    
    TUNING.NPC_UPGRADE_ITEMS_FN["cane"] = Fn
end
